package com.xiaoyao.novel.recovery;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.service.BookService;
import com.xiaoyao.novel.service.CategoryService;
import com.xiaoyao.novel.service.ChapterService;
import com.xiaoyao.novel.util.HttpClientUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class QqxxbdCrawl {

	public static String domain = "http://www.tianfengfeiye.com";
	public static HttpUtil httpUtil = HttpClientUtil.getDefault();
	static Logger logger = Logger.getLogger(QqxxbdCrawl.class);

	public static CategoryService categoryService = new CategoryService();
	public static BookService bookService = new BookService();
	public static ChapterService chapterService = new ChapterService();

	public static void crawlBookList() throws IOException {
		logger.info("抓取tianfengfeiye");
		List<Category> categories = categoryService.getAll();
		for (Category category : categories) {
			crawlBookList(category);
		}
		logger.info("抓取tianfengfeiye完毕");
	}

	public static List<Book> crawlBookList(Category category) throws IOException {
		// http://www.tianfengfeiye.com/list/7.html
		String urlStr = domain + "/list/" + category.getCateId() + ".html";
		String page = httpUtil.getPage(urlStr);
		List<Book> books = QqxxbdParser.parseBookList(page);
		for (int i = 0; i < books.size() ; i++) {
			logger.info("第"+(i+1)+"本书!" );
			Book book = books.get(i);
			
			book.setCategory(category.getCateName());
			crawlBook(book);
			logger.info(book);
			book = null;
		}

		return books;
	}
	public static void crawlBook(Book book) throws IOException {
		// http://www.tianfengfeiye.com/book/247187.html
		String urlStr = domain + "/book/" + book.bookId + ".html";
		String page = httpUtil.getPage(urlStr);
		List<Chapter> chapters = QqxxbdParser.parseBook(page, book.bookId);
		book.savedCount = chapters.size();
		logger.info("抓取书籍:"+book.bookId+" ,"+chapters.size()) ;
		for (Chapter chapter : chapters) {
			crawlChapter(chapter);
			book.setNewChapter(chapter.getChapterTitle());
		}
		bookService.insert(book);
	}

	public static void crawlChapter(Chapter chapter) throws IOException {
		logger.info("抓取章节:"+chapter.cid ) ;
		String urlStr = domain + "/chapter/" + chapter.cid + ".html";
		String page = httpUtil.getPage(urlStr);
		chapter = QqxxbdParser.parseChapter(page, chapter);
		chapterService.insert(chapter);
		chapter = null;
	}

}
